﻿using System;
using Silver3D.Math;

namespace Silver3D.Graphics
{
    /// <summary>
    /// Represents a rotation transformation defined by an angle (in radians) around an arbitrary axis.
    /// </summary>
    public class AngleAxisTransform : MatrixTransform
    {
        private double angle = 0.0;
        private Vec3 axis = Vec3.ZAxis;

        /// <summary>
        /// Gets or sets the rotation angle (in radians) around the Axis.
        /// The angle is contained within the range [0, 2pi].
        /// </summary>
        public double AngleRadians
        {
            get { return angle; }
            set
            {
                angle = value;
                Util.Wrap(ref angle, 0.0, Util.TwoPi);
                IsDirty = true;
            }
        }

        /// <summary>
        /// Gets or sets the rotation angle (in degrees) around the Axis.
        /// The angle is contained within the range [0, 360].
        /// </summary>
        public double AngleDegrees
        {
            get { return AngleRadians * Util.RadToDeg; }
            set { AngleRadians = value * Util.DegToRad; }
        }

        /// <summary>
        /// Gets or sets the rotation axis.
        /// </summary>
        public Vec3 Axis
        {
            get { return axis; }
            set
            {
                axis = value.Unit;
                IsDirty = true;
            }
        }

        public AngleAxisTransform(Scene scene, string id)
            : base(scene, id)
        {
        }

        public AngleAxisTransform(Scene scene, string id, Node parent)
            : base(scene, id, parent)
        {
        }

        public AngleAxisTransform(Scene scene, string id, Node parent, double angleInDegrees, Vec3 axis)
            : base(scene, id, parent)
        {
            AngleDegrees = angleInDegrees;
            Axis = axis;
        }

        /// <summary>
        /// Updates the CachedMatrix member with a Matrix44 that represents this rotation.
        /// </summary>
        protected override void UpdateMatrix()
        {
            CachedMatrix = Matrix44.CreateAngleAxis(angle, axis);
        }
    }
}
